#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#define MAX_TASKS 10
#define MAX_MEMORY 1024  // 最大内存大小，单位KB

// 任务结构体
typedef struct
{
    int taskId;            // 任务ID
    int priority;          // 任务优先级
    int estimatedTime;     // 估算的执行时间
    int remainingTime;     // 剩余执行时间
    int memoryNeeded;      // 内存需求，单位KB
    int status;            // 任务状态，0表示未执行，1表示正在执行，2表示已完成
} Task;

// 内存管理结构体
typedef struct
{
    int usedMemory;        // 当前已使用内存
} MemoryManager;

// 函数声明
void scheduleTasks(Task tasks[], int numTasks, MemoryManager *memoryManager);

void executeTask(Task *task, MemoryManager *memoryManager);

void allocateMemory(Task *task, MemoryManager *memoryManager);

void releaseMemory(Task *task, MemoryManager *memoryManager);

void printTaskStatus(Task tasks[], int numTasks);

int main()
{
    // 初始化任务
    Task tasks[MAX_TASKS] = {
            {1, 3, 5, 5, 100, 0},
            {2, 1, 3, 3, 200, 0},
            {3, 2, 4, 4, 150, 0},
            {4, 4, 2, 2, 50,  0},
            {5, 5, 6, 6, 300, 0}
    };
    int numTasks = 5;

    // 初始化内存管理
    MemoryManager memoryManager = {0};  // 初始内存使用为0

    // 调度任务
    scheduleTasks(tasks, numTasks, &memoryManager);

    // 打印任务状态
    printTaskStatus(tasks, numTasks);

    return 0;
}

// 调度任务
void scheduleTasks(Task tasks[], int numTasks, MemoryManager *memoryManager)
{
    // 按照优先级和估算时间排序，优先级高的任务先执行，如果优先级相同，选择估算时间短的任务
    for (int i = 0; i < numTasks - 1; i++)
    {
        for (int j = i + 1; j < numTasks; j++)
        {
            if (tasks[i].priority < tasks[j].priority ||
                (tasks[i].priority == tasks[j].priority && tasks[i].estimatedTime > tasks[j].estimatedTime))
            {
                // 交换任务
                Task temp = tasks[i];
                tasks[i] = tasks[j];
                tasks[j] = temp;
            }
        }
    }

    // 执行任务
    for (int i = 0; i < numTasks; i++)
    {
        // 为任务分配内存
        allocateMemory(&tasks[i], memoryManager);

        // 执行任务
        executeTask(&tasks[i], memoryManager);

        // 任务执行完毕后释放内存
        releaseMemory(&tasks[i], memoryManager);
    }
}

// 执行任务
void executeTask(Task *task, MemoryManager *memoryManager)
{
    // 设置任务为正在执行
    task->status = 1;
    printf("Executing Task ID: %d, Priority: %d, Estimated Time: %d, Memory Needed: %d KB\n",
           task->taskId, task->priority, task->estimatedTime, task->memoryNeeded);

    // 模拟任务执行
    sleep(task->estimatedTime);  // 休眠以模拟任务执行时间

    // 设置任务为已完成
    task->status = 2;
    printf("Task ID: %d has completed execution.\n", task->taskId);
}

// 为任务分配内存
void allocateMemory(Task *task, MemoryManager *memoryManager)
{
    if (memoryManager->usedMemory + task->memoryNeeded <= MAX_MEMORY)
    {
        memoryManager->usedMemory += task->memoryNeeded;
        printf("Allocated %d KB memory for Task ID: %d. Total used memory: %d KB\n",
               task->memoryNeeded, task->taskId, memoryManager->usedMemory);
    }
    else
    {
        printf("Not enough memory to allocate for Task ID: %d. Skipping task.\n", task->taskId);
        task->status = 0;  // 标记为跳过的任务
    }
}

// 释放任务占用的内存
void releaseMemory(Task *task, MemoryManager *memoryManager)
{
    memoryManager->usedMemory -= task->memoryNeeded;
    printf("Released %d KB memory for Task ID: %d. Total used memory: %d KB\n",
           task->memoryNeeded, task->taskId, memoryManager->usedMemory);
}

// 打印任务状态
void printTaskStatus(Task tasks[], int numTasks)
{
    printf("\nTask Status:\n");
    for (int i = 0; i < numTasks; i++)
    {
        printf("Task ID: %d, Priority: %d, Status: %s\n",
               tasks[i].taskId, tasks[i].priority,
               tasks[i].status == 0 ? "Skipped" : (tasks[i].status == 1 ? "Running" : "Completed"));
    }
}


